package com.hhh.springai_test.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.hhh.springai_test.common.BaseResponse;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.common.ResultUtils;
import com.hhh.springai_test.constant.UserConstant;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.model.dto.readingRoomReservation.AddReadingRoomReservationRequest;
import com.hhh.springai_test.model.dto.readingRoomReservation.UpdateReadingRoomReservationRequest;
import com.hhh.springai_test.model.po.ReadingRoomReservation;
import com.hhh.springai_test.model.vo.ReadingRoomReservationVO;
import com.hhh.springai_test.service.ReadingRoomReservationService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/readingRoomReservation")
public class ReadingRoomReservationController {

    @Resource
    private ReadingRoomReservationService readingRoomReservationService;

    @PostMapping("/add")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Long> addReadingRoomReservation(@RequestBody AddReadingRoomReservationRequest addRequest) {
        if (addRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long userId = StpUtil.getLoginIdAsLong();
        Long id = readingRoomReservationService.addReadingRoomReservation(addRequest,userId);
        return ResultUtils.success(id);
    }

    @PostMapping("/delete")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> deleteReadingRoomReservation(@RequestParam Long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不合法");
        }
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = readingRoomReservationService.deleteReadingRoomReservation(id,userId);
        return ResultUtils.success(result);
    }


    @GetMapping("/get")
    public BaseResponse<List<ReadingRoomReservation>> listReadingRoomReservation() {
        List<ReadingRoomReservation> list = readingRoomReservationService.listReadingRoomReservation();
        return ResultUtils.success(list);
    }

    @GetMapping("/my")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<List<ReadingRoomReservationVO>> getMyReservations() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<ReadingRoomReservationVO> reservations = readingRoomReservationService.getUserReservations(userId);
        return ResultUtils.success(reservations);
    }
}
